import * as NS from "../../Common/NetServices";
import { IsNull } from "../../TYLibrary/Common/GlobalExtend";
import { Wx_XSPPage, V_Goods, PageType } from "../../Common/Entitys";
import { Wx_PageData, WXDataClassRuntimeDecorator } from "../../Common/EntityBase";
import { WXIconType } from "../../wxAPI_";

@WXDataClassRuntimeDecorator()
class ProductPageData extends Wx_PageData {
    //商品
    public GoodsDetails: V_Goods = new V_Goods();
    public tabIndex: number = 0;
    public subTabIndex: number = 1;
    public bannerimgUrl: Array<string> = [
        "https://www.e-taiyun.cn/XCXFiles/xsp/img/pic/pic_07.jpg",
        "https://www.e-taiyun.cn/XCXFiles/xsp/img/pic/pic_08.jpg",
        "https://www.e-taiyun.cn/XCXFiles/xsp/img/pic/pic_09.jpg"
    ];
    public Current: number = 0;
    public ScrollTop: number = 0;
    public scrollHeight: number = 0;
    public IsShowBackTop: boolean = false;
    // ImgIntroduceList: `<img src="https://www.e-taiyun.cn/XCXFiles/xsp/img/pic/pic_06.jpg" class="img" />
    //     <img src="https://www.e-taiyun.cn/XCXFiles/xsp/img/pic/pic_20.jpg" class="img" />
    //     <img src="https://www.e-taiyun.cn/XCXFiles/xsp/img/pic/pic_26.jpg" class="img" />
    //     <img src="https://www.e-taiyun.cn/XCXFiles/xsp/img/pic/pic_31.jpg" class="img" />`,
    // ImgParameterList:
    //     '<img src="https://www.e-taiyun.cn/XCXFiles/xsp/img/pic/pic_23.jpg" class="img" />',
    // ImgPurchaseList:
    //     '<img src="https://www.e-taiyun.cn/XCXFiles/xsp/img/pic/pic_32.jpg" class="img"/>',
    public day: number = 0;
    public hour: number = 0;
    public minute: number = 0;
    public second: number = 0;
    public IsShow: boolean = false;
    public IsDisable: boolean = true;
    public Count: number = 1;
    public ParameterIndex: number = 0;
    public ColorIndex: number = 0;
}

class ProductPage extends Wx_XSPPage {
    private m_type = 0;
    private m_GoodsNo = null;
    public Data = new ProductPageData();

    /**
     * 初始化页面信息
     * @param {any} options
     * @returns {Promise<void>}
     * @memberof IndexPage
     */
    public async InitData(options: any): Promise<void> {
        try {
            wx.showLoading({
                title: "数据加载中..."
            });

            this.Data.scrollHeight = this.App.SystemInfo.windowHeight;
            this._Timer(2018, 5, 30);
            this.m_GoodsNo = options.GoodsNo;
            if (this.m_GoodsNo == null) {
                if (this.m_type != 0) this.App.WX_NavigateTo(PageType.AppIndex);
            } else this.App.Goods = await NS.QueryGoodsDetails(this.m_GoodsNo);
            this.Data.GoodsDetails = this.App.Goods;
            this.Data.ApplyChange(this);
        } catch (e) {
            this.ShowError(e);
        } finally {
            wx.hideLoading();
        }
    }

    /**
     *
     * @param {*} e
     * @memberof ProductPage
     */
    public ChangeHandler(e: any): void {
        let i = e.detail.current;
        this.Data.Current = i;
        this.Data.ApplyChange(this);
    }

    /**
     *
     * @param {*} e
     * @returns {Promise<void>}
     * @memberof ProductPage
     */
    public async scrolltolowerHandler(e: any): Promise<void> {
        this.Data.tabIndex = 1;
        this.Data.ApplyChange(this);
    }

    /**
     *
     * @param {*} e
     * @memberof ProductPage
     */
    public async ShowSubTabHandler(e: any): Promise<void> {
        const index = parseInt(e.currentTarget.dataset.subtabindex) + 1;
        console.log(typeof index, index);
        this.Data.subTabIndex = index;
        this.Data.ScrollTop = 0;
        this.Data.ApplyChange(this);
    }

    /**
     *
     * @param {*} e
     * @memberof ProductPage
     */
    public async ColorHandler(e: any): Promise<void> {
        const index = e.currentTarget.dataset.index;
        this.Data.ColorIndex = index;
        this.Data.ApplyChange(this);
    }

    /**
     *
     * @param {*} e
     * @memberof ProductPage
     */
    public async ParameterHandler(e: any): Promise<void> {
        const index = e.currentTarget.dataset.index;
        this.Data.ParameterIndex = index;
        this.Data.ApplyChange(this);
    }

    /**
     *
     * @param {*} e
     * @returns {Promise<void>}
     * @memberof ProductPage
     */
    public async GoBackTopHandler(e: any): Promise<void> {
        this.Data.ScrollTop = 0;
        this.Data.ApplyChange(this);
    }

    /**
     *
     * @param {*} e
     * @returns {Promise<void>}
     * @memberof ProductPage
     */
    public async ScrollHandler(e: any): Promise<void> {
        if (e.detail.scrollTop > 300) {
            this.Data.IsShowBackTop = true;
        } else {
            this.Data.IsShowBackTop = false;
        }
        this.Data.ApplyChange(this);
    }

    /**
     *
     * @param {*} e
     * @returns {Promise<void>}
     * @memberof ProductPage
     */
    public async AddShoppingHandler(e: any): Promise<void> {
        this.m_type = e.currentTarget.dataset.type;
        this.Data.IsShow = true;
        this.Data.ApplyChange(this);
    }

    /**
     *
     * @param {*} e
     * @returns {Promise<void>}
     * @memberof ProductPage
     */
    public async ConfirmCartHandler(e: any): Promise<void> {
        await NS.AddToCart(this.App.Goods.GOODSNO, this.Data.Count, this.App.Goods.SKUS[0].ID);
        this.CanlerHandler(e);
        wx.showToast({ title: "添加成功", icon: WXIconType.success });
        if (this.m_type != 0) {
            wx.navigateTo({ url: "../FirmOrder/Index" });
        }
    }

    /**
     *
     * @param {*} e
     * @returns {Promise<void>}
     * @memberof ProductPage
     */
    public async CanlerHandler(e: any): Promise<void> {
        this.Data.IsShow = false;
        this.Data.ApplyChange(this);
    }

    /**
     *
     * @param {*} e
     * @returns {Promise<void>}
     * @memberof ProductPage
     */
    public async NumberHandler(e: any): Promise<void> {
        let type = e.currentTarget.dataset.type;
        if (type == 0) {
            if (this.Data.Count == 2) {
                this.Data.IsDisable = true;
                this.Data.Count = 1;
            } else {
                if (this.Data.Count > 1) this.Data.Count--;
            }
            this.Data.ApplyChange(this);
        } else {
            this.Data.Count++;
            this.Data.IsDisable = false;
            this.Data.ApplyChange(this);
        }
    }

    /**
     *
     * @private
     * @param {number} year
     * @param {number} month
     * @param {number} day
     * @memberof ProductPage
     */
    private async _Timer(year: number, month: number, day: number) {
        let time = setInterval(() => {
            let now = new Date();
            let endDate = new Date(year, month - 1, day);
            let leftTime = endDate.getTime() - now.getTime();
            let leftsecond = Math.floor(leftTime / 1000);
            let day1 = Math.floor(leftsecond / (60 * 60 * 24));
            let hour = Math.floor((leftsecond - day1 * 24 * 60 * 60) / 3600);
            let minute = Math.floor((leftsecond - day1 * 24 * 60 * 60 - hour * 3600) / 60);
            let second = Math.floor(leftsecond - day1 * 24 * 60 * 60 - hour * 3600 - minute * 60);

            this.Data.day = day1;
            this.Data.hour = hour;
            this.Data.minute = minute;
            this.Data.second = second;
            this.Data.ApplyChange(this);
        }, 1000);
    }
}

Page(new ProductPage());
